﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace IntelligentCMS
{
    public partial class CMSFieldConfig : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            
            if (!this.Page.IsPostBack)
            {
                CMSDbDataLayer config = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                config.AddQuery("SELECT [Type], [ID] FROM FieldType");
                config.SelectQuery = true;
                config.Execute();

                for (int i = 0; i < config.Data.Rows.Count; i++)
                {

                    ListItem newItem = new ListItem(common.FromBase64String(config.Data.Rows[i]["Type"].ToString()), config.Data.Rows[i]["ID"].ToString());
                    DropDownList_Types.Items.Add(newItem);
                    //types.Add(newItem);
                }

                DropDownList_Types.SelectedValue = selectedvalue;
            }
            
            /*
            CMSDbDataLayer newDataLayer = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            newDataLayer.AddQuery("SELECT [Name], [ID] FROM FieldType");
            newDataLayer.NonQuery = false;
            newDataLayer.Execute();

            for (int i = 0; i < newDataLayer.Data.Rows.Count; i++)
            {
                DropDownList_Types.Items.Add(new ListItem(common.FromBase64String(newDataLayer.Data.Rows[i]["Name"].ToString()), newDataLayer.Data.Rows[i]["ID"].ToString()));

            }
             * 
             * */
            

        }

        //private List<ListItem> types = new List<ListItem>();

        private long fieldid = -1;


        /// <summary>
        /// ID of Field we are editing.
        /// </summary>
        public long FieldID
        {
            get
            {
                return fieldid;
            }

            set
            {
                fieldid = value;
            }

        }



        /// <summary>
        /// For future use. We can tell the control independently what to do.
        /// </summary>
        public string Operation
        {

            get
            {
                return operation;
            }
            set
            {
                operation = value;
            }
        }

        private int controlid = -1;

        /// <summary>
        /// ControlID -> This ID assigned to this particular control (useful for dynamically adding and removing controls from a page). 
        /// </summary>
        public int ControlID
        {
            get
            {
                return controlid;
            }
            set
            {
                controlid = value;
            }

        }

        private string operation = string.Empty;

        private string selectedvalue = string.Empty;

        public void Execute()
        {
            // Select
            CMSDbDataLayer field = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            field.AddQuery("SELECT [Name], [Description], [Tag], [FieldTypeID] FROM Field WHERE ID = " + fieldid);
            field.SelectQuery = true;
            field.Execute();
            
           
            TextBox_Name.Text = common.FromBase64String(field.Data.Rows[0]["Name"].ToString());
            TextBox_Description.Text = common.FromBase64String(field.Data.Rows[0]["Description"].ToString());
            TextBox_Tag.Text = common.FromBase64String(field.Data.Rows[0]["Tag"].ToString());

            //long test = long.Parse(newDataLayer.Data.Rows[0]["FieldTypeID"].ToString());

            selectedvalue = field.Data.Rows[0]["FieldTypeID"].ToString();


            /*
            ListItem ListItem = new ListItem();
            ListItem = DropDownList_Types.Items.FindByValue(newDataLayer.Data.Rows[0]["FieldTypeID"].ToString());

            if (ListItem == null)
            {
                DropDownList_Types.SelectedIndex = 0;
            }
            else
            {
                DropDownList_Types.SelectedValue = newDataLayer.Data.Rows[0]["FieldTypeID"].ToString();
            }
            */
            //DropDownList_Types.SelectedValue = newDataLayer.Data.Rows[0]["FieldTypeID"].ToString();

            //string value = DropDownList_Types.SelectedValue;
            

        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            // check for no input
            if (TextBox_Name.Text.Trim() == string.Empty || TextBox_Description.Text.Trim() == string.Empty || TextBox_Tag.Text.Trim() == string.Empty)
            {
                Label_Info.Text = "No textbox may be empty.";
                return;
            }



            // Insert
            if (fieldid == -1)
            {
                CMSDbDataLayer addfield = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                addfield.AddQuery("INSERT INTO [Field] ([Name], [Description], [FieldTypeID], [DateTime], [Tag]) VALUES ('" + common.GetBase64String(TextBox_Name.Text.Trim()) + "', '" + common.GetBase64String(TextBox_Description.Text.Trim()) + "', " + DropDownList_Types.SelectedValue + ", GETDATE(), '" + common.GetBase64String(TextBox_Tag.Text.Trim()) + "')");
                addfield.SelectQuery = false;
                addfield.Execute();


                CMSDbDataLayer field = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                field.AddQuery("SELECT [ID] FROM Field WHERE [Name] = '" + common.GetBase64String(TextBox_Name.Text.Trim()) + "'");
                field.SelectQuery = true;
                field.Execute();

                fieldid = long.Parse(field.Data.Rows[0]["ID"].ToString());
            }
            else
            {
            // Update

            }

            Label_Info.Text = "Field successfully saved.";

        }

        
    }
}